<?php
namespace Libs\Common;

use Symfony\Component\Process\Process;

class ConvertResizeImageCommon {
	/**
	 * delete all image by imageId
	 *
	 * @author  tinnt1 (tinnt1@vitalify.asia)
	 * @param	string	$sPath
	 * @param	integer	$imageId
	 * @param	string	$type (jpg, png, gif ...)
	 */
	public static function deleteImage($sPath, $imageId, $type = "jpg") {
		if (file_exists($sPath . $imageId . '.' . $type)) {
			unlink($sPath . $imageId . '.' . $type);
		}

		$aryListDemension = self::getDemensionList();
		foreach ($aryListDemension as $size) {
			$imageName = str_replace('[id]', $imageId, $size['name']);
			$imageNameOrg = str_replace('[id]', 'org_' . $imageId, $size['name']);
			if (file_exists($sPath . $imageName)) {
				unlink($sPath . $imageName);
			}
			if (file_exists($sPath . $imageNameOrg)) {
				unlink($sPath . $imageNameOrg);
			}
		}
	}

	/**
	 * delete all image by imageId
	 *
	 * @author  tinnt1 (tinnt1@vitalify.asia)
	 * @param	string	$sPath
	 * @param	integer	$imageId
	 * @param	string	$type (jpg, png, gif ...)
	 */
	public static function changePhotoAfterAprroveDeny($sPath, $imageId, $type = "jpg") {

		// delete photo original
		if (file_exists($sPath . 'pc_smallphoto' . '_org_' . $imageId . '.' . $type)) {
			// if (file_exists($sPath . 'pc_smallphoto' . '_' . $imageId . '.' . $type)) {
				// unlink($sPath . 'pc_smallphoto' . '_' . $imageId . '.' . $type);
			// }
			// rename($sPath . 'pc_smallphoto' . '_org_' . $imageId . '.' . $type, $sPath . 'pc_smallphoto' . '_' . $imageId . '.' . $type);
			copy($sPath . 'pc_smallphoto' . '_org_' . $imageId . '.' . $type, $sPath . 'pc_smallphoto' . '_' . $imageId . '.' . $type);
		}
		if (file_exists($sPath . 'pc_largephoto' . '_org_' . $imageId . '.' . $type)) {
			// if (file_exists($sPath . 'pc_largephoto' . '_' . $imageId . '.' . $type)) {
				// unlink($sPath . 'pc_largephoto' . '_' . $imageId . '.' . $type);
			// }
			// rename($sPath . 'pc_largephoto' . '_org_' . $imageId . '.' . $type, $sPath . 'pc_largephoto' . '_' . $imageId . '.' . $type);
			copy($sPath . 'pc_largephoto' . '_org_' . $imageId . '.' . $type, $sPath . 'pc_largephoto' . '_' . $imageId . '.' . $type);
		}
		if (file_exists($sPath . 'pc_listphoto' . '_org_' . $imageId . '.' . $type)) {
			// if (file_exists($sPath . 'pc_listphoto' . '_' . $imageId . '.' . $type)) {
				// unlink($sPath . 'pc_listphoto' . '_' . $imageId . '.' . $type);
			// }
			// rename($sPath . 'pc_listphoto' . '_org_' . $imageId . '.' . $type, $sPath . 'pc_listphoto' . '_' . $imageId . '.' . $type);
			copy($sPath . 'pc_listphoto' . '_org_' . $imageId . '.' . $type, $sPath . 'pc_listphoto' . '_' . $imageId . '.' . $type);
		}
		if (file_exists($sPath . 'sp_smallphoto' . '_org_' . $imageId . '.' . $type)) {
			// if (file_exists($sPath . 'sp_smallphoto' . '_' . $imageId . '.' . $type)) {
				// unlink($sPath . 'sp_smallphoto' . '_' . $imageId . '.' . $type);
			// }
			// rename($sPath . 'sp_smallphoto' . '_org_' . $imageId . '.' . $type, $sPath . 'sp_smallphoto' . '_' . $imageId . '.' . $type);
			copy($sPath . 'sp_smallphoto' . '_org_' . $imageId . '.' . $type, $sPath . 'sp_smallphoto' . '_' . $imageId . '.' . $type);
		}
		if (file_exists($sPath . 'sp_largephoto' . '_org_' . $imageId . '.' . $type)) {
			// if (file_exists($sPath . 'sp_largephoto' . '_' . $imageId . '.' . $type)) {
				// unlink($sPath . 'sp_largephoto' . '_' . $imageId . '.' . $type);
			// }
			// rename($sPath . 'sp_largephoto' . '_org_' . $imageId . '.' . $type, $sPath . 'sp_largephoto' . '_' . $imageId . '.' . $type);
			copy($sPath . 'sp_largephoto' . '_org_' . $imageId . '.' . $type, $sPath . 'sp_largephoto' . '_' . $imageId . '.' . $type);
		}
		if (file_exists($sPath . 'sp_listphoto' . '_org_' . $imageId . '.' . $type)) {
			// if (file_exists($sPath . 'sp_listphoto' . '_' . $imageId . '.' . $type)) {
				// unlink($sPath . 'sp_listphoto' . '_' . $imageId . '.' . $type);
			// }
			// rename($sPath . 'sp_listphoto' . '_org_' . $imageId . '.' . $type, $sPath . 'sp_listphoto' . '_' . $imageId . '.' . $type);
			copy($sPath . 'sp_listphoto' . '_org_' . $imageId . '.' . $type, $sPath . 'sp_listphoto' . '_' . $imageId . '.' . $type);
		}
	}

	/**
	 * Implement watermark into photo & create org photo
	 *
	 * @author TinNT1 (tinnt1@vitalify.asia)
	 * create date 2014/4/18
	 * modify date 2014/4/23
	 */
	public static function watermarkImage($sPath, $sImage, $listDemension = array()) {
		$typeimg = substr($sImage, strrpos($sImage, '.'));
		$imgname = str_replace($typeimg, '', $sImage);
		foreach ($listDemension as $size) {
			// create watermark
			// ======================= TINNT1 - 23/4/2104 (tinnt1@vitalify.asia) =========================
			$imagePath = $sPath . str_replace('[id]', $imgname, $size['name']);
			$newfile = $sPath . str_replace('[id]', 'org_' . $imgname, $size['name']);
			$waterPath = SERVER_ROOT_FOLDER_USER_IMGS . 'watermark_' . $size['w'] . 'x' . $size['h'] . ".png";

			if (!copy($imagePath, $newfile)) {
				echo "failed to copy $newfile...\n";
			}
			// Load the requested image
			$image = imagecreatefromstring(file_get_contents($imagePath));
			$w = imagesx($image);
			$h = imagesy($image);

			// Load the watermark image
			$watermark = imagecreatefrompng($waterPath);
			$ww = imagesx($watermark);
			$wh = imagesy($watermark);

			$dst_x = (int) (($w - $ww) / 2);
			$dst_y = (int) (($h - $wh) / 2);
			// Merge watermark upon the original image
			imagecopy($image, $watermark, $dst_x, $dst_y, 0, 0, $ww, $wh);
			imagejpeg($image, $imagePath, 100);
			//============================= END TINNT1 ===================================================
		}
	}

	/**
     * Convert & upload & resize all format of iamge to jpg for PC
     * @author ThamLT
     * @param sPath link to image
     * @param sImage images file
     * @param listDemension list all size of image want to resize
     *          listDemension[] = array('w' => 123, 'h' => 123, 'q' => 70, 'thumb' => 1, or 0 not thumb, type='pc or sp'), q is quality
     * */
    public static function resizeImagetoJpg($sPath, $sImage, $listDemension = array())
    {
        if (!empty($listDemension)) {
            if (!file_exists($sPath . $sImage)) {
                return false;
            }
            $process = new Process('chmod 777 ' . $sPath);
            $process -> run();
            unset($process);
            $process = new Process('chmod 777 ' . $sPath . $sImage);
            $process -> run();
            unset($process);

            list($width, $height, $type, $attr) = getimagesize($sPath . $sImage);
            $typeimg = substr($sImage, strrpos($sImage, '.'));

            $imgname = str_replace($typeimg, '', $sImage);

            $fRateRealImageSize = $width / $height;
            //if real image is square only resize for thumb
            foreach ($listDemension as $ix=>$size) {
                if ($size['thumb'] == 0) {
                    $sCommand = CONVERT_IMAGEMAGIC . ' ' . $sPath . $sImage;
                    if ($size['w'] == $size['h']) {
                        if ($width > $height) {
                            $sCommand .= ' -thumbnail x' . $size['w'] . ' ';
                        } else {
                            $sCommand .= ' -thumbnail ' . $size['w'] . ' ';
                        }
                    } else {
                        $fRateCurSize = $size['w'] / $size['h'];

                        if ($fRateRealImageSize > $fRateCurSize) {
                            if ($fRateRealImageSize > 1) {//w > h

                                if ($size['w'] > $size['h']) {
                                    $sCommand .= ' -thumbnail ' . $size['w'] . ' ';
                                } elseif ($size['w'] < $size['h']) {
                                    $sCommand .= ' -thumbnail x' . $size['h'] . ' ';
                                } else {
                                    if ($size['w'] < $size['h']) {
                                        $sCommand .= ' -thumbnail x' . $size['h'] . ' ';
                                    } else {
                                        $sCommand .= ' -thumbnail ' . $size['w'] . ' ';
                                    }
                                }
                            } else {//w <= h
                                if ($width > $height) {
                                    $sCommand .= ' -thumbnail x' . $size['w'] . ' ';
                                } elseif ($width < $height) {
                                    $sCommand .= ' -thumbnail x' . $size['h'] . ' ';
                                } else {
                                    if ($size['w'] < $size['h']) {
                                        $sCommand .= ' -thumbnail x' . $size['h'] . ' ';
                                    } else {
                                        $sCommand .= ' -thumbnail ' . $size['h'] . ' ';
                                    }
                                }
                            }
                        } else {
                            if ($width > $height) {
                                $sCommand .= ' -thumbnail x' . $size['h'] . ' ';
                            } elseif ($width < $height) {
                                $sCommand .= ' -thumbnail ' . $size['w'] . ' ';
                            } else {
                                if ($size['w'] < $size['h']) {
                                    $sCommand .= ' -thumbnail x' . $size['h'] . ' ';
                                } else {
                                    $sCommand .= ' -thumbnail ' . $size['w'] . ' ';
                                }
                            }
                        }
                    }
                    $sCommand .= ' -quality ' . $size['q'] . ' ';
                    $sCommand .= ' ' . $sPath . $imgname . '_tmp'.$ix.'.jpg';
//echo '<p>'.$sCommand.'</p>';
                    $process = new Process($sCommand);
                    $process -> run();
                    unset($process);

                    if (file_exists($sPath . $imgname . '_tmp'.$ix.'.jpg')) {
                        //chmod for image have just created
                        $process = new Process('chmod 777 ' . $sPath . $imgname . '_tmp'.$ix.'.jpg');
                        $process -> run();
                        unset($process);
                        $sCommand = CONVERT_IMAGEMAGIC . ' ' . $sPath . $imgname . '_tmp'.$ix.'.jpg';

                        if ($width == $height) {
                            //$sCommand .= ' -gravity center -crop ';
                            $sCommand .= ' -repage 0x0 -crop ';
                            $sCommand .= $size['w'] . 'x' . $size['h'] . '\!';

                            list($widthtmp, $heighttmp, $typetmp, $attrtmp) = getimagesize($sPath . $imgname . '_tmp'.$ix.'.jpg');
                            if ($widthtmp > $size['w']) {
                                //$sCommand .= '+'.round(($widthtmp - $size['w']) /2, 0) . ' +repage -quality ' . $size['q'] . ' ';
                                $sCommand .= '+'.round(($widthtmp - $size['w']) /2, 0) . ' -quality ' . $size['q'] . ' ';
                            } else {
                                //$sCommand .= '+0+0 +repage -quality ' . $size['q'] . ' ';
                                $sCommand .= '+0+0 -quality ' . $size['q'] . ' ';
                            }
                        } else {
                            $sCommand .= ' -repage 0x0 -crop ';
                            $sCommand .= $size['w'] . 'x' . $size['h'] . '\!';
                            //$sCommand .= '+0+0 +repage -quality ' . $size['q'] . ' ';
                            list($widthtmp, $heighttmp, $typetmp, $attrtmp) = getimagesize($sPath . $imgname . '_tmp'.$ix.'.jpg');
                            if ($widthtmp > $size['w']) {
                                //$sCommand .= '+'.round(($widthtmp - $size['w']) /2, 0) . ' +repage -quality ' . $size['q'] . ' ';
                                $sCommand .= '+'.round(($widthtmp - $size['w']) /2, 0) . ' -quality ' . $size['q'] . ' ';
                            } else {
                                //$sCommand .= '+0+0 +repage -quality ' . $size['q'] . ' ';
                                $sCommand .= '+0+0 -quality ' . $size['q'] . ' ';
                            }
                        }

                        $imgPath = $sPath . str_replace('[id]', $imgname, $size['name']);
                        $sCommand .= $imgPath;
                        //echo '<p>'.$sCommand.'</p>';
                        //convert image
                        $process = new Process($sCommand);
                        $process -> run();
                        unset($process);
                        $process = new Process('chmod 777 ' . $imgPath);
                        $process -> run();
                        unset($process);
                        unlink($sPath . $imgname . '_tmp'.$ix.'.jpg');
                    }

                    /*$sCommand = CONVERT_IMAGEMAGIC . ' ' . $sPath . $sImage;
                    if ($size['w'] > $size['h'] && $width > $height) {
                        $sCommand .= ' -thumbnail ' . $size['w'] . ' ';
                    } elseif ($size['w'] < $size['h'] && $width > $height) {
                        $sCommand .= ' -thumbnail ' . $size['h'] . ' ';
                    } elseif ($size['w'] > $size['h'] && $width < $height) {
                        $sCommand .= ' -thumbnail x' . $size['w'] . ' ';
                    } elseif ($size['w'] < $size['h'] && $width < $height) {
                        $sCommand .= ' -thumbnail x' . $size['h'] . ' ';
                    } elseif ($size['w'] == $size['h'] && $width > $height) {
                        $sCommand .= ' -thumbnail ' . $size['w'] . ' ';
                    } elseif ($size['w'] == $size['h'] && $width < $height) {
                        $sCommand .= ' -thumbnail x' . $size['w'] . ' ';
                    } elseif ($width == $height) {
                        if ($size['w'] > $size['h']) {
                            $sCommand .= ' -thumbnail x' . $size['h'] . ' ';
                        } else {
                            $sCommand .= ' -thumbnail ' . $size['w'] . ' ';
                        }
                    }
                    $sCommand .= ' -quality ' . $size['q'] . ' ';
                    $sCommand .= ' ' . $sPath . $imgname . '_tmp1.jpg';
                    $process = new Process($sCommand);
                    $process -> run();
                    unset($process);

                    if (file_exists($sPath . $imgname . '_tmp1.jpg')) {
                        //chmod for image have just created
                        $process = new Process('chmod 777 ' . $sPath . $imgname . '_tmp1.jpg');
                        $process -> run();
                        unset($process);
                        $sCommand = CONVERT_IMAGEMAGIC . ' ' . $sPath . $imgname . '_tmp1.jpg';

                        $sCommand .= ' -resize ';
                        $sCommand .= $size['w'] . 'x' . $size['h'];
                        $sCommand .= ' -quality ' . $size['q'] . ' ';

                        $imgPath = $sPath . str_replace('[id]', $imgname, $size['name']);
                        $sCommand .= $imgPath;
                        //convert image
                        $process = new Process($sCommand);
                        $process -> run();
                        unset($process);
                        $process = new Process('chmod 777 ' . $imgPath);
                        $process -> run();
                        unset($process);
                        unlink($sPath . $imgname . '_tmp1.jpg');
                    }*/

                } else {

                    $sCommand = CONVERT_IMAGEMAGIC . ' ' . $sPath . $sImage;
                    if ($size['w'] == $size['h']) {
                        if ($width > $height) {
                            $sCommand .= ' -thumbnail x' . $size['w'] . ' ';
                        } else {
                            $sCommand .= ' -thumbnail ' . $size['w'] . ' ';
                        }
                    } else {
                        $fRateCurSize = $size['w'] / $size['h'];

                        if ($fRateRealImageSize > $fRateCurSize) {
                            if ($fRateRealImageSize > 1) {//w > h
                                if ($size['w'] > $size['h']) {
                                    $sCommand .= ' -thumbnail ' . $size['w'] . ' ';
                                } elseif ($size['w'] < $size['h']) {
                                    $sCommand .= ' -thumbnail x' . $size['h'] . ' ';
                                } else {
                                    if ($size['w'] < $size['h']) {
                                        $sCommand .= ' -thumbnail x' . $size['h'] . ' ';
                                    } else {
                                        $sCommand .= ' -thumbnail ' . $size['w'] . ' ';
                                    }
                                }
                            } else {//w <= h
                                if ($width > $height) {
                                    $sCommand .= ' -thumbnail x' . $size['w'] . ' ';
                                } elseif ($width < $height) {
                                    $sCommand .= ' -thumbnail ' . $size['h'] . ' ';
                                } else {
                                    if ($size['w'] < $size['h']) {
                                        $sCommand .= ' -thumbnail x' . $size['h'] . ' ';
                                    } else {
                                        $sCommand .= ' -thumbnail ' . $size['h'] . ' ';
                                    }
                                }
                            }
                        } else {
                            if ($width > $height) {
                                $sCommand .= ' -thumbnail x' . $size['h'] . ' ';
                            } elseif ($width < $height) {
                                $sCommand .= ' -thumbnail ' . $size['w'] . ' ';
                            } else {
                                if ($size['w'] < $size['h']) {
                                    $sCommand .= ' -thumbnail x' . $size['h'] . ' ';
                                } else {
                                    $sCommand .= ' -thumbnail ' . $size['w'] . ' ';
                                }
                            }
                        }
                    }

                    /*if ($size['w'] > $size['h'] && $width > $height) {
                        $sCommand .= ' -thumbnail ' . $size['w'] . ' ';
                    } elseif ($size['w'] < $size['h'] && $width > $height) {
                        if ($size['h'] > $height) {
                            $sCommand .= ' -thumbnail x' . $size['h'] . ' ';
                        } else {
                            $sCommand .= ' -thumbnail ' . $size['h'] . ' ';
                        }
                    } elseif ($size['w'] > $size['h'] && $width < $height) {
                        $sCommand .= ' -thumbnail ' . $size['w'] . ' ';
                    } elseif ($size['w'] < $size['h'] && $width < $height) {
                        $sCommand .= ' -thumbnail ' . $size['h'] . ' ';
                    } elseif ($size['w'] == $size['h'] && $width > $height) {
                        if ($size['w'] > $width && $size['thumb'] == 0) {
                            $expend = abs($width - $size['w']);
                            $sCommand .= ' -thumbnail ' . ($size['w'] + $expend) . ' ';
                        } else {
                            $sCommand .= ' -thumbnail x' . $size['w'] . ' ';
                        }
                    } elseif ($size['w'] == $size['h'] && $width < $height) {
                        if ($size['w'] > $width && $size['thumb'] == 0) {
                            $expend = abs($width - $size['w']);
                            $sCommand .= ' -thumbnail x' . ($size['w'] + $expend) . ' ';
                        } else {
                            $sCommand .= ' -thumbnail ' . $size['w'] . ' ';
                        }
                    } elseif ($width == $height) {
                        if ($size['w'] < $size['h']) {
                            $sCommand .= ' -thumbnail ' . $size['h'] . ' ';
                        } else {
                            $sCommand .= ' -thumbnail ' . $size['w'] . ' ';
                        }
                    }*/
                    $sCommand .= ' -quality ' . $size['q'] . ' ';
                    $sCommand .= ' ' . $sPath . $imgname . '_tmp'.$ix.'.jpg';
//echo '<p>'.$sCommand.'</p>';
                    $process = new Process($sCommand);
                    $process -> run();
                    unset($process);

                    if (file_exists($sPath . $imgname . '_tmp'.$ix.'.jpg')) {
                        //chmod for image have just created
                        $process = new Process('chmod 777 ' . $sPath . $imgname . '_tmp'.$ix.'.jpg');
                        $process -> run();
                        unset($process);
                        $sCommand = CONVERT_IMAGEMAGIC . ' ' . $sPath . $imgname . '_tmp'.$ix.'.jpg';

                        if ($width == $height) {
                            //$sCommand .= ' -gravity center -crop ';
                            $sCommand .= ' -repage 0x0 -crop ';
                            $sCommand .= $size['w'] . 'x' . $size['h'] . '\!';

                            list($widthtmp, $heighttmp, $typetmp, $attrtmp) = getimagesize($sPath . $imgname . '_tmp'.$ix.'.jpg');
                            if ($widthtmp > $size['w']) {
                                //$sCommand .= '+'.round(($widthtmp - $size['w']) /2, 0) . ' +repage -quality ' . $size['q'] . ' ';
                                $sCommand .= '+'.round(($widthtmp - $size['w']) /2, 0) . ' -quality ' . $size['q'] . ' ';
                            } else {
                                //$sCommand .= '+0+0 +repage -quality ' . $size['q'] . ' ';
                                $sCommand .= '+0+0 -quality ' . $size['q'] . ' ';
                            }
                        } else {
                            $sCommand .= ' -repage 0x0 -crop ';
                            $sCommand .= $size['w'] . 'x' . $size['h'] . '\!';
                            //$sCommand .= '+0+0 +repage -quality ' . $size['q'] . ' ';
                            list($widthtmp, $heighttmp, $typetmp, $attrtmp) = getimagesize($sPath . $imgname . '_tmp'.$ix.'.jpg');
                            if ($widthtmp > $size['w']) {
                                //$sCommand .= '+'.round(($widthtmp - $size['w']) /2, 0) . ' +repage -quality ' . $size['q'] . ' ';
                                $sCommand .= '+'.round(($widthtmp - $size['w']) /2, 0) . ' -quality ' . $size['q'] . ' ';
                            } else {
                                //$sCommand .= '+0+0 +repage -quality ' . $size['q'] . ' ';
                                $sCommand .= '+0+0 -quality ' . $size['q'] . ' ';
                            }
                        }

                        $imgPath = $sPath . str_replace('[id]', $imgname, $size['name']);
                        $sCommand .= $imgPath;
                        //echo '<p>'.$sCommand.'</p>';
                        //convert image
                        $process = new Process($sCommand);
                        $process -> run();
                        unset($process);
                        $process = new Process('chmod 777 ' . $imgPath);
                        $process -> run();
                        unset($process);
                        unlink($sPath . $imgname . '_tmp'.$ix.'.jpg');
                    }
                }
            }

            if (file_exists($sPath . $imgname . '_tmp1.jpg')) {
                unlink($sPath . $imgname . '_tmp1.jpg');
            }
        }
    }

	/**
	 * Upload image from url of facebook to our server
	 * @author ThamLT
	 * @param fbid
	 *         id of Update SubPhoto
	 * */
	public static function uploadPhotoFromUrlFacebook($fbid, $photoId, $sUid) {
		try {
			//get url of facebook profile, this url will redirect to other link
			// so we have to get response header to get right link
			$ctx = stream_context_create(array('http' => array('timeout' => 1200)));
			if (file_get_contents('https://graph.facebook.com/' . $fbid . '/picture?width=9999', false, $ctx) === false) {
				return false;
			}

			/*
			 * $http_response_header ==> default variable of server, index include:
			 * 1: Location include url (needed)
			 * 2: extensiond....
			 */
			$findlocation = '';
			foreach ($http_response_header as $item) {
				if (strstr($item, 'Location')) {
					$findlocation = trim(str_replace('Location:', '', $item));
					break;
				}
			}

			if (!empty($findlocation)) {
				$url = $findlocation;
				$typeimg = substr($url, strrpos($url, '.'));
				//$pathdir = dirname(dirname(dirname(dirname(__DIR__)))) . UPLOAD_USER . $sUid . '/';
				$pathdir = SERVER_ROOT_FOLDER_USER_IMGS . $sUid . '/';
				if (!file_exists($pathdir)) {
					mkdir($pathdir);
				}

				//upload image from url to server

				file_put_contents($pathdir . $photoId . $typeimg, file_get_contents($url));
				if (file_exists($pathdir . $photoId . $typeimg)) {
					//conver to jpg & resize image have just upload to all size in spec
					$listDemension = self::getDemensionList();
					self::resizeImagetoJpg($pathdir, $photoId . $typeimg, $listDemension);
					return true;
				} else {
					return false;
				}
			}
		} catch(Exception $e) {
			return false;
		}
	}

	/**
	 * Upload image from url to our server
	 * @author ThamLT
	 * @param updatePHotoId
	 *          $sImage = 'heehllo.jpg'
	 *         id of Update SubPhoto
	 * */
	public static function uploadPhotoFromUrl($pathdir, $sImage, $photoId, $sUid) {
		if (file_exists($pathdir . $sImage)) {

			//conver to jpg & resize image have just upload to all size in spec
			$listDemension = self::getDemensionList();
			self::resizeImagetoJpg($pathdir, $sImage, $listDemension);
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Upload image from url to our server
	 * @author ThamLT
	 * @param updatePHotoId
	 *          $sImage = 'heehllo.jpg'
	 *         id of Update SubPhoto
	 * */
	public static function uploadPhotoLicenseFromUrl($pathdir, $sImage, $photoId, $sUid) {
		if (file_exists($pathdir . $sImage)) {

			//conver to jpg & resize image have just upload to all size in spec
			$listDemension = self::getDemensionList();
			self::resizeImagePhotoLicense($pathdir, $sImage, $listDemension);
			return true;
		} else {
			return false;
		}
	}

     /**
      * Resize all formage of image to jpg
      * @author ThamlT
      * */
     public static function resizeImagePhotoLicense($sPath, $sImage, $listDemension = array())
     {
         if (!empty($listDemension)) {
            if (!file_exists($sPath . $sImage)) {
                return false;
            }
            $process = new Process('chmod 777 ' . $sPath);
            $process -> run();
            unset($process);
            $process = new Process('chmod 777 ' . $sPath . $sImage);
            $process -> run();
            unset($process);

            list($width, $height, $type, $attr) = getimagesize($sPath . $sImage);
            $typeimg = substr($sImage, strrpos($sImage, '.'));

            $imgname = str_replace($typeimg, '', $sImage);

            //if real image is square only resize for thumb
            foreach ($listDemension as $ix=>$size) {
                $sCommand = CONVERT_IMAGEMAGIC . ' ' . $sPath . $sImage;
                $sCommand .= ' -resize ' . $size['w'] . 'x' . $size['w'];
                $sCommand .= ' -quality ' . $size['q'];

                $imgPath = $sPath . str_replace('[id]', $imgname, $size['name']);

                $sCommand .= ' ' . $imgPath;
                 //convert image
                $process = new Process($sCommand);
                $process->run();
                unset($process);
                $process = new Process('chmod 777 ' . $imgPath);
                $process->run();
                unset($process);
            }
        }
     }

	/**
	 * Get all demension of project
	 * @author ThamLT
	 * */
	public static function getDemensionList() {
		//Make demension for both PC & SP
		$listDemension = array();
		$listDemension[] = array('w' => RESIZE_PC_SMALL_WIDTH, 'h' => RESIZE_PC_SMALL_HEIGHT, 'name' => 'pc_smallphoto_[id].jpg', 'q' => 100, 'thumb' => 1);
		$listDemension[] = array('w' => RESIZE_PC_LARGE_WIDTH, 'h' => RESIZE_PC_LARGE_HEIGHT, 'name' => 'pc_largephoto_[id].jpg', 'q' => 100, 'thumb' => 0);
		$listDemension[] = array('w' => RESIZE_LISTPHOTO_WIDTH, 'h' => RESIZE_LISTPHOTO_HEIGHT, 'name' => 'pc_listphoto_[id].jpg', 'q' => 100, 'thumb' => 2);

		$listDemension[] = array('w' => RESIZE_SP_SMALL_WIDTH, 'h' => RESIZE_SP_SMALL_HEIGHT, 'name' => 'sp_smallphoto_[id].jpg', 'q' => 100, 'thumb' => 1);
		$listDemension[] = array('w' => RESIZE_SP_LARGE_WIDTH, 'h' => RESIZE_SP_LARGE_HEIGHT, 'name' => 'sp_largephoto_[id].jpg', 'q' => 100, 'thumb' => 0);
		$listDemension[] = array('w' => RESIZE_LISTPHOTO_WIDTH, 'h' => RESIZE_LISTPHOTO_HEIGHT, 'name' => 'sp_listphoto_[id].jpg', 'q' => 100, 'thumb' => 2);
		return $listDemension;
	}

}
